#include <iostream>
#include <vector>
using namespace std;


class Solution 
{
public:
    int maxScore(vector<int>& cardPoints, int k)
    {
        int sum = 0, n = cardPoints.size(), len = n - k, add = 0;
        for (int i = 0; i < len; i++)
        {
            sum += cardPoints[i];
            add += cardPoints[i];
        }

        int min_sum = sum, j = 0;
        for (int i = len; i < n; i++)
        {
            add += cardPoints[i];
            sum += cardPoints[i] - cardPoints[j++];
            min_sum = min(min_sum, sum);
        }

        return add - min_sum;
    }
};


int main()
{
    vector<int> nums = { 1,2,3,4,5,6,1 };
    int k = 3;
    cout << Solution().maxScore(nums, k);
    return 0;
}